// API基础配置
const API_BASE_URL = 'http://localhost:3001/api'

// 通用请求函数
const request = async (url, options = {}) => {
  try {
    const response = await fetch(`${API_BASE_URL}${url}`, {
      headers: {
        'Content-Type': 'application/json',
        ...options.headers
      },
      ...options
    })

    const data = await response.json()

    if (!response.ok) {
      throw new Error(data.message || `HTTP error! status: ${response.status}`)
    }

    return data
  } catch (error) {
    console.error('API请求失败:', error)
    throw error
  }
}

// 合作伙伴相关API
export const partnersAPI = {
  // 获取所有合作伙伴
  getAll: (params = {}) => {
    const queryString = new URLSearchParams(params).toString()
    const url = queryString ? `/partners?${queryString}` : '/partners'
    return request(url)
  },

  // 获取单个合作伙伴
  getById: (id) => {
    return request(`/partners/${id}`)
  },

  // 添加合作伙伴
  create: (data) => {
    return request('/partners', {
      method: 'POST',
      body: JSON.stringify(data)
    })
  },

  // 更新合作伙伴
  update: (id, data) => {
    return request(`/partners/${id}`, {
      method: 'PUT',
      body: JSON.stringify(data)
    })
  },

  // 删除合作伙伴
  delete: (id) => {
    return request(`/partners/${id}`, {
      method: 'DELETE'
    })
  }
}

// 关于我们相关API
export const aboutAPI = {
  // 获取所有关于我们的数据
  getAll: () => {
    return request('/about')
  },

  // 获取公司信息
  getCompanyInfo: () => {
    return request('/about/company')
  },

  // 获取发展历程
  getMilestones: () => {
    return request('/about/milestones')
  },

  // 获取核心价值
  getCoreValues: () => {
    return request('/about/values')
  },

  // 获取团队成员
  getTeamMembers: () => {
    return request('/about/team')
  },

  // 获取联系信息
  getContactInfo: () => {
    return request('/about/contact')
  },

  // 更新公司信息
  updateCompanyInfo: (data) => {
    return request('/about/company', {
      method: 'PUT',
      body: JSON.stringify(data)
    })
  }
}

// 服务相关API
export const servicesAPI = {
  // 获取所有服务数据
  getAll: () => {
    return request('/services')
  },

  // 获取服务类型
  getServiceTypes: () => {
    return request('/services/types')
  },

  // 获取客服信息
  getCustomerService: () => {
    return request('/services/customer')
  },

  // 获取常见问题
  getFAQ: (params = {}) => {
    const queryString = new URLSearchParams(params).toString()
    const url = queryString ? `/services/faq?${queryString}` : '/services/faq'
    return request(url)
  },

  // 获取售后服务
  getSupportServices: () => {
    return request('/services/support')
  },

  // 获取咨询服务
  getConsultationServices: () => {
    return request('/services/consultation')
  },

  // 获取服务承诺
  getServicePromise: () => {
    return request('/services/promise')
  },

  // 提交咨询预约
  submitConsultationBooking: (data) => {
    return request('/services/consultation/booking', {
      method: 'POST',
      body: JSON.stringify(data)
    })
  },

  // 更新服务数据
  updateServices: (data) => {
    return request('/services', {
      method: 'PUT',
      body: JSON.stringify(data)
    })
  }
}

// 默认导出
export default {
  partnersAPI,
  aboutAPI,
  servicesAPI
}
